home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / IO Examples / IO Utilities / Highscore.icl < prev    next >
Encoding:
Modula Implementation  |  1997-04-28  |  3.4 KB  |  112 lines  |  [TEXT/3PRM]

  1. implementation module Highscore
  2.  
  3.  
  4. /*
  5.     General utility for reading/writing high scores to Files and displaying current high scores.
  6.     This module uses the 0.8 I/O library.
  7. */
  8.  
  9. import    StdBool, StdString, StdFile, StdEnum, StdTuple, StdList, StdMisc
  10. from    deltaSystem    import HomePath
  11. import    deltaIOSystem, deltaDialog
  12.  
  13. ::    *HiScores    :== (!Files,!Highs)
  14. ::    Highs        :== [High]
  15. ::    High
  16.     =    {    name    :: !String
  17.         ,    score    :: !Int
  18.         }
  19.  
  20. //    Read in the high scores:
  21. ReadHiScores :: !String !Files -> (!*File,!HiScores)
  22. ReadHiScores fname files
  23. #    (exists,file,files)    = fopen fpath FReadData files
  24. |    exists                = (file1, (files,highs))
  25.                         with
  26.                             (highs,file1)    = ReadHighs file
  27.                             
  28.                             ReadHighs :: !*File -> (!Highs,!*File)
  29.                             ReadHighs file
  30.                             |    sfend file    = ([],file)
  31.                             #    (name, file)= freads file 13
  32.                                 (ok,hi,file)= freadi file
  33.                             |    not ok        = ([],file)
  34.                             #    (ok,_, file)= freadc file
  35.                             |    not ok        = ([],file)
  36.                             #    (rest, file)= ReadHighs file
  37.                             |    otherwise    = ([{name=name,score=hi}:rest],file)
  38. #    (_,create,files)    = fopen fpath FWriteData files
  39. |    otherwise            = (create,(files,[]))
  40. where
  41.     fpath                = HomePath fname
  42.  
  43. //    Write the high scores:
  44. WriteHiScores :: !*File !HiScores -> Files
  45. WriteHiScores file (files,highs)
  46. #    (ok,file)    = freopen file FWriteData
  47. |    not ok        = abort "Could not reopen file.\n"
  48. #    file        = WriteHighs highs file
  49.     (_,files)    = fclose file files
  50. =    files
  51. where
  52.     WriteHighs :: !Highs !*File -> *File
  53.     WriteHighs [{name,score}:scores] file
  54.     #    file    = fwrites  (ThirteenCharString name) file
  55.         file    = fwritei  score                     file
  56.         file    = fwritec '\n'                         file
  57.         file    = WriteHighs scores                     file
  58.     =    file
  59.     WriteHighs _ file = file
  60.     
  61.     ThirteenCharString :: !String -> String
  62.     ThirteenCharString string = (string+++"             ")%(0,12)
  63.  
  64.  
  65. //    Determine whether, given the number of high scores, a given score is actually a new high score:
  66. ItsAHighScore :: !Int !Int !Highs -> Bool
  67. ItsAHighScore nrOfHiScores score scores
  68. |    score==0                    = False
  69. |    length scores<nrOfHiScores    = True
  70. |    otherwise                    = IsItReallyAHighScore score scores
  71. where
  72.     IsItReallyAHighScore :: !Int !Highs -> Bool
  73.     IsItReallyAHighScore score` [{score}:hiscores]
  74.     |    score`>score            = True
  75.     |    otherwise                = IsItReallyAHighScore score` hiscores
  76.     IsItReallyAHighScore _ _    = False
  77.  
  78.  
  79. //    Add a High to the current list of high scores:
  80. AddScore :: !Int !High !Highs -> Highs
  81. AddScore nrOfHighScores hi hiscores
  82. =    take nrOfHighScores (addscore hi hiscores)
  83. where
  84.     addscore :: !High !Highs -> Highs
  85.     addscore hi` hiscores=:[hi:his]
  86.     |    hi.score>hi`.score    = [hi  : addscore hi` his]
  87.     |    otherwise            = [hi` : hiscores]
  88.     addscore hi` _            = [hi`]
  89.  
  90.  
  91. //    Display high scores in a modal dialog to the user:
  92. ShowHiScores :: DialogId String !Highs !*s !(IOState *s) -> (!*s,!IOState *s)
  93. ShowHiScores _ _ [] state io
  94. #    (_,state,io)    = OpenNotice (Notice ["No high scores available."] (NoticeButton 1 "OK") []) state io
  95. =    (state,io)
  96. ShowHiScores id header highs state io
  97. =    OpenModalDialog dialog state io
  98. where
  99.     dialog    = CommandDialog id "High Scores" [] okId
  100.                 [    StaticText 1 Center header
  101.                 :    flatten
  102.                 [    
  103.                 [    DynamicText id        (YOffset (id-2) (Pixel 2)) (MM 65.0) (toString ((id-2)/2)+++". "+++name)
  104.                 ,    StaticText  (id+1)    (XOffset id (MM 0.0)) (toString score)
  105.                 ]    \\ (id,{name,score}) <- zip2 [4,6..] highs
  106.                 ]
  107.                 ++    
  108.                 [    DialogButton okId Center "OK" Able (\_ state io -> (state, CloseActiveDialog io))
  109.                 ]
  110.                 ]
  111.     okId    = 2*(length highs+1)
  112.